package com.gfxy.modules.classtime.service.impl;

import com.gfxy.modules.classtime.entity.ClasstimeReport;
import com.gfxy.modules.classtime.entity.GfxyTClasstimeUpload;
import com.gfxy.modules.classtime.mapper.GfxyTClasstimeUploadMapper;
import com.gfxy.modules.classtime.service.IClasstimeReportService;
import com.gfxy.modules.classtime.service.IGfxyTClasstimeUploadService;
import com.gfxy.modules.classtime.service.IClasstimeScoreService;
import com.gfxy.modules.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.*;
import java.util.*;

/**
 * @Description: 课时文件上传
 * @Author: jeecg-boot
 * @Date:   2021-05-25
 * @Version: V1.0
 */
@Slf4j
@Service
public class GfxyTClasstimeUploadServiceImpl extends ServiceImpl<GfxyTClasstimeUploadMapper, GfxyTClasstimeUpload> implements IGfxyTClasstimeUploadService {

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;


    @Autowired
    private IClasstimeReportService classtimeReportService;

    @Autowired
    private IClasstimeScoreService classtimeScoreService;

    @Override
    public void importData(String year, String month, String savePath) {

        try {
            log.info("开始导入{}年-{}月的课时数据， 课时数据文件为:{}", year,month,savePath);
            String ctxPath = uploadpath;
            File file = new File(ctxPath + File.separator + savePath );
            InputStream ips = new FileInputStream(file);
            Workbook workbook= WorkbookFactory.create(ips);
            Iterator<Sheet> sheetIterator = workbook.sheetIterator();
            while (sheetIterator.hasNext()){
                Sheet sheet = sheetIterator.next();
                String name = sheet.getSheetName();
                log.debug("sheet name is {}" , name);

                //todo 确定sheet 使用名字还是顺序
                if(name.equals("全院教师") || name.equals("全院") || name.equals("全院汇总") || name.equals("全院教师汇总") || name.equals("全校教师汇总")){
                    int startRow = 2;
                    List<ClasstimeReport> reportList = new ArrayList<>();
//                    System.out.println(sheet.getLastRowNum());
                    for( ; startRow<sheet.getLastRowNum(); startRow++){
                        Row currentRow = sheet.getRow(startRow);

                        ClasstimeReport report = new ClasstimeReport();
                        report.setDate(year);
                        report.setMonth(month);

                        if(currentRow!=null){
                            String teacherId = ExcelUtil.formatCell(currentRow.getCell(4));
                            if(teacherId != null && !teacherId.isEmpty()){
                                report.setTeacherCode(teacherId);
                                report.setTotalClasstime(transformString(ExcelUtil.formatCell(currentRow.getCell(6))));
                                //理论
                                report.setLecture(transformString(ExcelUtil.formatCell(currentRow.getCell(7))));
                                //课程设计
                                report.setCourseDesign(transformString(ExcelUtil.formatCell(currentRow.getCell(8))));
                                //实践
                                report.setPractical(transformString(ExcelUtil.formatCell(currentRow.getCell(9))));
                                //毕设指导
                                report.setGraduateTutorial(transformString(ExcelUtil.formatCell(currentRow.getCell(10))));
                                //毕设答辩
                                report.setGraduateDefense(transformString(ExcelUtil.formatCell(currentRow.getCell(11))));

                                double other = transformString(ExcelUtil.formatCell(currentRow.getCell(12)));
//                            other += transformString(ExcelUtil.formatCell(currentRow.getCell(14)));
//                            other += transformString(ExcelUtil.formatCell(currentRow.getCell(15)));
//                            other += transformString(ExcelUtil.formatCell(currentRow.getCell(16)));
//                            other += transformString(ExcelUtil.formatCell(currentRow.getCell(17)));

                                //考试
//                            report.setExamination(transformString(ExcelUtil.formatCell(currentRow.getCell(14))));
//                            //顶岗
//                            report.setInternship(transformString(ExcelUtil.formatCell(currentRow.getCell(15))));
//                            report.setPracticalTutorial(transformString(ExcelUtil.formatCell(currentRow.getCell(16))));
//                            report.setOther(transformString(ExcelUtil.formatCell(currentRow.getCell(17))));
                                report.setOther(other);
//                            System.out.println(report);
                                reportList.add(report);
                            }

                        }

                    }

                    Map<String, Object> columnMap = new HashMap<>();
                    columnMap.put("date",year);
                    columnMap.put("month",month);
                    classtimeReportService.removeByMap(columnMap);
                    classtimeReportService.saveBatch(reportList);
                } else {
                    log.error("未找到合适标签，请检查标签名称");
                }
            }
            log.debug("导入完成，开始计算积分");
            classtimeScoreService.computeClasstimePoints(year);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(),e);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

    public Double transformString(String s){

        if("".equals(s)){
            return 0.0;
        } else {
            System.out.println(s);
            return Double.parseDouble(s);
        }
    }



}
